/*
 * @lc app=leetcode id=297 lang=cpp
 *
 * [297] Serialize and Deserialize Binary Tree
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Codec {
public:

    // Encodes a tree to a single string.
    string serialize(TreeNode* root) {
        return toString(toVector(root));
    }

    vector<TreeNode *> toVector(TreeNode *root) {

        vector<TreeNode *> result;
        if (!root) {
            return result;
        }
        
        result.push_back(root);
        vector<TreeNode *> dq = {root};

        while(!dq.empty()) {
            vector<TreeNode *> tmp;
            for (auto n : dq) {

                result.push_back(n->left);
                result.push_back(n->right);

                if (n->left) {
                    tmp.push_back(n->left);
                }
                if (n->right) {
                    tmp.push_back(n->right);
                }
            }
            dq = tmp;
        }
        return result;
    }

    string toString(vector<TreeNode *> nodes) {
        stringstream ss;
        ss << "[";

        int len = nodes.size();
        while(len > 0 && nodes[len-1] == NULL)  {
            len--;
        }

        if (len > 0) {
            ss << nodes[0]->val;
            for (int i=1; i<len; i++) {
                ss << ",";
                if (nodes[i]) {
                    ss << nodes[i]->val;
                } else {
                    ss << "null";
                }
            }
        }

        ss << "]";
        string output;
        ss >> output;
        return output;
    }

    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
        int len = data.size();
        if (len <= 2 || !(data[0] == '[' && data[len-1]==']')) {
            return NULL;
        }
        string s = data.substr(1, len-2);
        len = s.size();

        TreeNode *node = NULL;

        vector<TreeNode *> nodes;
        int i = 0, pos = 0;
        while(pos < len) {
            i = findNext(s, len, pos);
            if (i <= pos) {     // end
                break;
            }

            string sval = s.substr(pos, i-pos);
            if (sval == "") {               // end
                break;
            }

            if (sval == "null") {
                node = NULL;
            } else {
                node = new TreeNode(stoi(sval));
            }

            nodes.push_back(node);
            pos = i+1;
        }

        return buildTree(nodes);
    }

    TreeNode *buildTree(vector<TreeNode *>& nodes) {
        TreeNode *root = NULL;
        int len = nodes.size();
        if (len < 1) {
            return root;
        }

        int i = 1;
        root = nodes[0];
        vector<TreeNode *> dq = {root};

        while(i < len) {
            vector<TreeNode *> tmp;
            for (auto n : dq) {
                if (i < len) {
                    n->left = nodes[i];
                    if (n->left) {
                        tmp.push_back(n->left);
                    }
                    i++;
                }

                if (i<len) {
                    n->right = nodes[i];
                    if (n->right) {
                        tmp.push_back(n->right);
                    }
                    i++;
                }
            }
            dq = tmp;
        }
        return root;
    }

    int findNext(string& data, int len, int pos) {
        int i = pos;
        for (; i<len; i++) {
            if (data[i] == ',') {
                break;
            }
        }
        return i;
    }
};

// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));

